package com.anzia.study_12;

import com.anzia.study_12.data.BookStore;
import com.anzia.study_12.data.User;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.resultset.ResultSet;
import ohos.data.rdb.ValuesBucket;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class SimpleDataAbility extends Ability {
	private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
	private OrmContext ormContext = null;
	OrmContext bookStore;

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		HiLog.info(LABEL_LOG, "SimpleDataAbility onStart");

		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		bookStore = databaseHelper.getOrmContext("BookStore", "BookStore.db", BookStore.class, null);

	}

	@Override
	public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
		if (ormContext == null) {
			return null;
		}
		if (!uri.getDecodedPathList().get(1).equals("User")) {
			return null;
		}
		// 真正查询数据的语句
		OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, User.class);
		ResultSet resultSet = ormContext.query(ormPredicates, columns);
		return resultSet;
	}

	@Override
	public int insert(Uri uri, ValuesBucket value) {
		HiLog.info(LABEL_LOG, "SimpleDataAbility insert");
		if (ormContext == null) {
			return -1;
		}
		if (!uri.getDecodedPathList().get(1).equals("User")) {
			return -1;
		}
		User user = new User();
		user.setFirstName(value.getString("firstName"));
		user.setLastName(value.getString("lastName"));
		user.setAge(value.getInteger("age"));
		user.setBalance(value.getDouble("balance"));

		boolean insert = ormContext.insert(user);
		if (!insert) {
			return -1;
		}
		boolean flush = ormContext.flush();
		if (!flush) {
			return -1;
		}
		int id = Math.toIntExact(user.getRowId());
		return id;
	}

	@Override
	public int delete(Uri uri, DataAbilityPredicates predicates) {
		if (ormContext == null) {
			return -1;
		}
		if (!uri.getDecodedPathList().get(1).equals("User")) {
			return -1;
		}
		OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, User.class);
		int delete = ormContext.delete(ormPredicates);
		return delete;
	}

	@Override
	public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
		if (ormContext == null) {
			return -1;
		}
		if (!uri.getDecodedPathList().get(1).equals("User")) {
			return -1;
		}
		OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, User.class);
		int update = ormContext.update(ormPredicates, value);
		return update;
	}

	@Override
	public FileDescriptor openFile(Uri uri, String mode) {
		return null;
	}

	@Override
	public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
		return new String[0];
	}

	@Override
	public PacMap call(String method, String arg, PacMap extras) {
		return null;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}
}